package mbms.mobilebudget.bankaccount;

import java.sql.SQLException;
import java.util.List;

import mbms.mobilebudget.database.DatabaseHandler;
import mbms.mobilebudget.entity.BankAccount;
import mbms.mobilebudget.entity.Expense;
import mbms.mobilebudget.entity.Income;
import mbms.mobilebudget.entity.User;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.j256.ormlite.dao.Dao;

@SuppressLint("ShowToast")
public class BankAccountManager {

	private DatabaseHandler helper;
	private Context context;

	public BankAccountManager(DatabaseHandler helper, Context context) {
		this.helper = helper;
		this.context = context;
	}
	
	public BankAccount addBankAccount(BankAccount account, User authenticatedUser){
		account.setUser(authenticatedUser);
		account.setTotalIncomeAmount(0.0);
		account.setTotalExpenseAmount(0.0);
		try {
			Dao<BankAccount,Integer> bankAccountDao = helper.getBankAccountDao();
			bankAccountDao.create(account);
			Log.d("Insertion",bankAccountDao.queryForId(account.getId()).getId()+ " id inserted");
		} catch (SQLException e) {
			e.printStackTrace();
			Log.e("error", "add bank account");
			return null;
		}
		return account;
	}
	
	public BankAccount updateBankAccount(BankAccount account){
		try {
			Dao<BankAccount,Integer> bankAccountDao = helper.getBankAccountDao();
			BankAccount updatedAccount= bankAccountDao.queryForId(account.getId());
			if(updatedAccount==null || updatedAccount.getId()==null){
				Toast.makeText(context, "id not retrieved while updating",
						Toast.LENGTH_LONG);
				return null;
			}
			else{
				copyBankAccount(account, updatedAccount);
				bankAccountDao.update(updatedAccount);
					Toast.makeText(context, "update with id : " + updatedAccount.getId(),
							Toast.LENGTH_LONG);
				return updatedAccount;
			}
		} catch (SQLException e) {
			Log.e("error", "update bank account");
			Log.e("error", e.getStackTrace().toString());
			return null;
		}
		
		
	}
	public BankAccount deleteBankAccount(BankAccount account){
		try {
			Dao<BankAccount,Integer> bankAccountDao = helper.getBankAccountDao();
			BankAccount toBeDeleted= bankAccountDao.queryForId(account.getId());
			if(toBeDeleted==null || toBeDeleted.getId()==null){
				Toast.makeText(context, "id not retrieved while deleting", Toast.LENGTH_LONG);
				return null;
			}
			else{
				bankAccountDao.delete(toBeDeleted);
					Toast.makeText(context, "deleted with id : " + toBeDeleted.getId(),
							Toast.LENGTH_LONG);
				return toBeDeleted;
			}
		} catch (SQLException e) {
			Log.e("error", "delete bank account");
		}
		return account;
		
	}
	
	public List<Income> getIncomeListOfBankAccount(BankAccount bankAccount) {
		if(bankAccount == null || bankAccount.getId()==null){
			Toast.makeText(context, "list failed",
					Toast.LENGTH_LONG);
			return null;
		}
		try {
			Dao<Income, Integer> incomeDao = helper.getIncomeDao();
			List <Income> incomes = incomeDao.queryForEq("bankAccount_id", bankAccount.getId());
			return incomes;
		} catch (SQLException e) {
			Log.e("list", "error");
			e.printStackTrace();
		}
		
		return null;
	}
	
	public List<Expense> getExpenseListOfBankAccount(BankAccount bankAccount) {
		if(bankAccount == null || bankAccount.getId()==null){
			Toast.makeText(context, "list failed", Toast.LENGTH_LONG);
			return null;
		}
		try {
			Dao<Expense, Integer> expenseDao = helper.getExpenseDao();
			List <Expense> expenses = expenseDao.queryForEq("bankAccount_id", bankAccount.getId());
			if(expenses!=null){
				for (Expense exp : expenses) {
					Log.d("List", exp.getId() + " exists with amout "+
							exp.getAmount());
				}
				return expenses;
			}
			
		} catch (SQLException e) {
			Log.e("list", "error");
			e.printStackTrace();
		}
		
		return null;
		
	}
	
	private void copyBankAccount(BankAccount gives, BankAccount takes){
		takes.setAccountNumber(gives.getAccountNumber());
		takes.setAccountType(gives.getAccountType());
		takes.setBankName(gives.getBankName());
		takes.setBranch(gives.getBranch());
		takes.setCurrencyType(gives.getCurrencyType());
		takes.setIBAN(gives.getIBAN());
		
	}
}
